﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Nova_Common;
using Nova_MOD;
using Nova_BLL;
using Nova_DAL;
using System.Data.SqlClient;
using System.Data.Common;

public partial class Views_CargoStatus : System.Web.UI.Page
{
    string lbl_ZoneID = "";//库区号

    string lbl_LaneNo = ""; //巷道号
    string cid = "";        ////层
    int rid = 0;        //gridview行的编号
    int iLcol = 0;
    string PosID = "";
    int colArea = 60;


    DataSet ds = null;
    DataTable myTable = null;
    int PosState = 0;           //货位状态
    int iLineNo = 0;               //排
    int iLayNo = 0;           //层
    string PID = "";
    DbHelper access = new DbHelper();
    DbCommand cmd = null;


    Nova_Common.LoginLogic cLoginLogic = new LoginLogic();
    Nova_BLL.TBase_Position Nova_BLL_TBase_Position = new Nova_BLL.TBase_Position();
    Nova_BLL.TLaneDefine Nova_BLL_TLaneDefine = new Nova_BLL.TLaneDefine();
    Nova_BLL.TPosState Nova_BLL_TPostState = new Nova_BLL.TPosState();
    Nova_MOD.TBase_Position Nova_MOD_TBase_Position = new Nova_MOD.TBase_Position();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Nova_Common.LoginLogic.IsLogin();
            ddlNo_Lanebind(); //加载 巷道
            BindDllZone();     //加载库区
            dlistPosbind();
            gvbind();

        }
        lblMsg.Text = "";
    }

    private void BindDllZone()
    {
        string sql = string.Format(@"select Zone_ID,Zone_Name from TBase_Zone where Structure <> 0 order by Zone_ID
            ");

        this.cmd = this.access.GetSqlStringCommond(sql);
        ds = this.access.ExecuteDataSet(this.cmd);

        ddlZone.DataTextField = "Zone_Name";
        ddlZone.DataValueField = "Zone_ID";
        ddlZone.DataSource = ds;
        ddlZone.DataBind();
        //ddlZone.Items.Insert(0, new ListItem("所有自动化库", "0"));
    }

    /// <summary>
    /// 加载 巷道
    /// </summary>
    protected void ddlNo_Lanebind()
    {
        ds = Nova_BLL_TLaneDefine.DataSetTLaneDefine();
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            ddlNo_Lane.DataSource = ds;
            ddlNo_Lane.DataValueField = "No_Lane";
            ddlNo_Lane.DataTextField = "No_Lane_Text";
            ddlNo_Lane.DataBind();
        }
    }
    protected bool reInt(string strNum)
    {
        try
        {
            int var1 = Convert.ToInt32(strNum);
            return true;
        }
        catch
        {
            return false;
        }
    }

    protected void btn_Click(object sender, EventArgs e)
    {
        int subArea = int.Parse(ddlNo_Col.SelectedValue);
        Button btn = (Button)sender;
        if (!string.IsNullOrEmpty(btn.CommandArgument.Trim()))
        {
            int ibtn = int.Parse(btn.CommandArgument);//修改的货位状态值
            if (txtELcol.Text == "" || txtELine.Text == "" || txtSLcol.Text == "" || txtSLine.Text == "")//有些问题
            {
                ShowBox.MessageBox(this.Page, "请选择开始坐标和结束坐标！");
                return; 
            }
            else
            {
                for (int i = Convert.ToInt32(txtSLine.Text); i <= Convert.ToInt32(txtELine.Text); i++)
                {
                    for (int j = Convert.ToInt32(txtSLcol.Text); j <= Convert.ToInt32(txtELcol.Text); j++)
                    {
                        PosState = Convert.ToInt32(Nova_BLL_TPostState.intPosID(GV.Rows[i - 1].Cells[j].Text.ToString()));       //取货位状态
                        lbl_LaneNo = ddlNo_Lane.SelectedValue.ToString();       //巷道号
                        iLineNo = Convert.ToInt32(GV.Rows[i - 1].Cells[0].Text.Substring(0, 1));     //排
                        iLcol = j;         //列
                        string strLayNo = GV.Rows[i - 1].Cells[1].Text;
                        iLayNo = Convert.ToInt32(strLayNo.Substring(0, strLayNo.Length - 1));     //层

                        Nova_MOD_TBase_Position.No_Lane = int.Parse(lbl_LaneNo);
                        Nova_MOD_TBase_Position.No_Line = iLineNo;
                        Nova_MOD_TBase_Position.No_Col = subArea == 1 ? iLcol : (colArea + iLcol);
                        Nova_MOD_TBase_Position.No_Layer = iLayNo;
                        PID = Nova_BLL_TBase_Position.GetiPID(Nova_MOD_TBase_Position, ddlZone.SelectedValue.ToString());
                        string strMessage = Nova_BLL_TBase_Position.UpdatePosition(PID, ibtn);

                        if (strMessage != "")
                        {
                            lblMsg.Text = strMessage;
                        }
                    }
                }
            }
            txtELcol.Text = "";
            txtELine.Text = "";
            txtSLcol.Text = "";
            txtSLine.Text = "";
            gvbind();
        }
    }

    /// <summary>
    /// 增加库区的属性
    /// xgw edit for 解决视图加载卡
    /// </summary>
    protected void gvbind()
    {
        lbl_ZoneID = ddlZone.SelectedValue.ToString();
        lbl_LaneNo = ddlNo_Lane.SelectedValue.ToString();
        int subArea = int.Parse(ddlNo_Col.SelectedValue);
        //myTable = Nova_BLL_TBase_Position.DTableTBase_PositionZoneID(lbl_ZoneID, lbl_LaneNo);
        myTable = Nova_BLL_TBase_Position.DTableTBase_PositionZoneIDPosImg(lbl_ZoneID, lbl_LaneNo, subArea);
        if (myTable != null && myTable.Rows.Count > 0)
        {
            GV.DataSource = myTable;
            GV.DataBind();
        }
    }

    protected void ddlNo_Lane_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvbind();
    }

    protected void ddlZone_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvbind();
    }

    protected void GV_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        #region
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    e.Row.Cells[1].Attributes["style"] = "width:30px;";
        //    cid = e.Row.Cells[1].Text.ToString().Substring(0, 1);//层
        //    rid = e.Row.RowIndex + 1;  //行号
        //    lbl_ZoneID = ddlZone.SelectedValue.ToString();       //库区号
        //    lbl_LaneNo = ddlNo_Lane.SelectedValue.ToString();       //巷道号
        //    iLineNo = Convert.ToInt32(e.Row.Cells[0].Text.Substring(0, 1));     //排
        //    //iLcol = e.Row.RowIndex;         //列
        //    iLayNo = Convert.ToInt32(e.Row.Cells[1].Text.Substring(0, 1));     //层
        //    DataRowView dv = (DataRowView)e.Row.DataItem;
        //    for (int i = 0; i < e.Row.Cells.Count; i++)
        //    {
        //        if (i == 0)
        //        {
        //            iLcol = 1;
        //        }
        //        else if (i == 1)
        //        {
        //            iLcol = 1;
        //        }
        //        else
        //        {
        //            iLcol = i-1;
        //        }

        //        if (i > 1)//if (i > 2)
        //        {
        //            if (e.Row.Cells[i].Text.ToString() == "1")
        //            {
        //                PosID = lbl_ZoneID.PadLeft(2, '0') + lbl_LaneNo.PadLeft(2, '0') + iLineNo.ToString() + iLcol.ToString().PadLeft(3, '0') + iLayNo.ToString().PadLeft(2, '0') + "00";
        //                //e.Row.Cells[i].Attributes["ondblclick"] = string.Format("window.showModalDialog('StorageList.aspx?PosID={0}', window, 'dialogWidth=900px;dialogHeight=450px;scroll:no;resizable:no;status:0;');",PosID);
        //            }
        //            e.Row.Cells[i].Text = Nova_BLL_TPostState.strPosImg(e.Row.Cells[i].Text.ToString());

        //        }



        //        e.Row.Cells[i].Attributes["onclick"] = "showEdit(" + lbl_LaneNo + "," + e.Row.Cells[0].Text.ToString().Substring(0, 1) + "," + iLcol + "," + cid + "," + rid + ");";


        //    }
        //}
        #endregion


        //每行绑定  点击行行触发SelectedIndexChanging事件（SelectedIndexChanging 不能自动触发）
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["style"] = "cursor:hand";

            #region   //点击行触发SelectedIndexChanging事件
            //PostBackOptions myPostBackOptions = new PostBackOptions(this);
            //myPostBackOptions.AutoPostBack = false;
            //myPostBackOptions.PerformValidation = false;
            //myPostBackOptions.RequiresJavaScriptProtocol = true; //加入javascript:头
            //String evt = Page.ClientScript.GetPostBackClientHyperlink(sender as GridView, "Select$" + e.Row.RowIndex.ToString());
            // e.Row.Attributes.Add("onclick", evt);

            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            e.Row.ToolTip = "Click to select row";
            e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(sender as GridView, "Select$" + e.Row.RowIndex);

            #endregion
        }
    }

    //索引变更事件--为选择的索引行的所有单元格 绑定单击功能（文本框显示单元格的行、列）
    protected void GV_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        this.GV.SelectedIndex = e.NewSelectedIndex;
        int index = e.NewSelectedIndex;
        string rid = (index + 1).ToString(); //行号
        //GV.Rows[index].BackColor = System.Drawing.Color.FromName("#CCCCCC"); //选中的行显示灰色

        for (int i = 0; i < this.GV.Rows[index].Cells.Count; i++)
        {

            //列的数据获取处理
            //if (i < 2)
            //{
            //    iLcol = 1;
            //}
            //else
            //{
            //    iLcol = i - 1;
            //}
            if (i < 2)
            {
                iLcol = 1;
            }
            else
            {
                iLcol = i - 1;
            }

            this.GV.Rows[index].Cells[i].Attributes["onclick"] = "showEdit(" + iLcol + "," + rid + ");";
        }
    }


    protected void dlistPosbind()
    {
        ds = Nova_BLL_TPostState.DataListPosState();
        if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
        {
            dlistPos.DataSource = ds;
            dlistPos.DataBind();
        }
    }

    protected void ibtnSelect_Click(object sender, ImageClickEventArgs e)
    {
        //开始的坐标不能为空，结束的坐标为空，只处理开始坐标的那一个单元格
        if (txtSLine.Text != "" && txtSLcol.Text != "")
        {
            if (!reInt(txtSLine.Text))
            {
                ShowBox.MessageBox(this.Page, "开始坐标请输入数字!");
                return;
            }
            if (!reInt(txtSLcol.Text))
            {
                ShowBox.MessageBox(this.Page, "开始坐标请输入数字!");
                return;
            }
            if (txtELine.Text != "" && txtELine.Text != "")
            {
                if (!reInt(txtELine.Text))
                {
                    ShowBox.MessageBox(this.Page, "结束坐标请输入数字!");
                    return;
                }
                if (!reInt(txtELcol.Text))
                {
                    ShowBox.MessageBox(this.Page, "结束坐标请输入数字!");
                    return;
                }

                int SLine = Convert.ToInt32(txtSLine.Text.ToString());
                int ELine = Convert.ToInt32(txtELine.Text.ToString());
                int iLine = 0;

                int SLcol = Convert.ToInt32(txtSLcol.Text.ToString());
                int ELcol = Convert.ToInt32(txtELcol.Text.ToString());
                int iLcol = 0;

                if (SLine > ELine)
                {
                    txtSLine.Text = ELine.ToString();
                    txtELine.Text = SLine.ToString();
                    iLine = SLine;
                    SLine = ELine;
                    ELine = iLine;

                }

                if (SLcol > ELcol)
                {
                    txtSLcol.Text = ELcol.ToString();
                    txtELcol.Text = SLcol.ToString();
                    iLcol = SLcol;
                    SLcol = ELcol;
                    ELcol = iLcol;
                }

                for (int x = SLine; x <= ELine; x++)
                {
                    for (int j = SLcol; j <= ELcol; j++)
                    {
                        //GV.Rows[x - 1].Cells[j].BackColor = System.Drawing.Color.FromName("#ff0000");
                        GV.Rows[x - 1].Cells[j + 1].BackColor = System.Drawing.Color.FromName("#ff0000");
                    }
                }
            }
            else
            {
                //GV.Rows[Convert.ToInt32(txtSLine.Text) - 1].Cells[Convert.ToInt32(txtSLcol.Text)].BackColor = System.Drawing.Color.FromName("#ff0000");
                GV.Rows[Convert.ToInt32(txtSLine.Text) - 1].Cells[Convert.ToInt32(txtSLcol.Text) + 1].BackColor = System.Drawing.Color.FromName("#ff0000");
            }
        }
    }
    protected void ibtnReset_Click(object sender, ImageClickEventArgs e)
    {
        txtELcol.Text = "";
        txtELine.Text = "";
        txtSLcol.Text = "";
        txtSLine.Text = "";
        gvbind();
    }

    //protected void btnzhupan_Click(object sender, EventArgs e)
    //{
    //    int iResNo = 0;
    //    string strResMsg = "";

    //    if (txtPos_ID.Text == "0" || txtPos_ID.Text == "")
    //    {
    //        ShowBox.MessageBox(this.Page, "请填写货位号！");
    //    }
    //    else
    //    {
    //        cmd = access.GetStoredProcCommond("SP_ASRS_Generate_Out_CMDS_ByPos");
    //        access.AddInParameter(cmd, "@PosS_ID", DbType.String, txtPos_ID.Text.ToString().Trim());
    //        access.AddInParameter(cmd, "@SQIDE", DbType.String, 0);
    //        access.AddInParameter(cmd, "@USERNAME", DbType.String, Request.Cookies["vNovaUserNo"].Value.ToString());
    //        access.AddOutParameter(cmd, "@RESNO", DbType.Int32, 100);
    //        access.AddOutParameter(cmd, "@RESMSG", DbType.String, 255);
    //        access.ExecuteNonQuery(cmd);

    //        iResNo = Convert.ToInt32(access.GetParameter(cmd, "@RESNO").Value.ToString());
    //        strResMsg = access.GetParameter(cmd, "@RESMSG").Value.ToString();
    //        if (iResNo > 0)
    //        {
    //            Label1.Text = "整出成功！";
    //        }
    //        else
    //        {
    //            Label1.Text = "整出失败！";
    //            Label1.Text += strResMsg;
    //        }
    //    }
    //}

    //protected void btnpandian_Click(object sender, EventArgs e)
    //{
    //    int iResNo = 0;

    //    cmd = access.GetStoredProcCommond("SP_ASRS_Generate_PanDian_CMDS_ByPos");
    //    access.AddInParameter(cmd, "@PosS_ID", DbType.String, txtPos_ID.Text.ToString().Trim());
    //    access.AddInParameter(cmd, "@USERNAME", DbType.String, Request.Cookies["vNovaUserNo"].Value.ToString());
    //    access.AddOutParameter(cmd, "@RESNO", DbType.Int32, 100);
    //    access.AddOutParameter(cmd, "@RESMSG", DbType.String, 255);
    //    iResNo = access.ExecuteNonQuery(cmd);
    //    if (iResNo > 0)
    //    {
    //        Label1.Text = "盘点成功！";
    //    }
    //    else
    //    {
    //        Label1.Text = "盘点失败！";
    //    }
    //}


    protected void ddlNo_Col_SelectedIndexChanged(object sender, EventArgs e)
    {
        gvbind();
    }
}
